﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;
using System.Windows.Markup;

namespace SymbolEditor.WpfLibrary
{
    /// <summary>
    /// dlgSymbolEditor.xaml 的交互逻辑
    /// </summary>
    public partial class dlgSymbolEditor : Window
    {
        public dlgSymbolEditor()
        {
            InitializeComponent();
        }

        private Graphic graphic = null;

        public string RDHander = string.Empty;
        public string RDFooter = string.Empty;

        public Graphic Graphic
        {
            set
            {
                InitialGraphicEditor(value);
            }
            get { return graphic; }
        }

        private void InitialGraphicEditor(Graphic value)
        {
            //field
            graphic = value;

            //layer
            GraphicsLayer graphicslayer = MyMap.Layers["SymbolViewer"] as GraphicsLayer;
            graphicslayer.Graphics.Clear();
            graphicslayer.Graphics.Add(graphic);

            MyMap.Extent = graphic.Geometry.Extent;

            //textbox
            tbSymbolID.Text = graphic.Attributes["KEY"].ToString();
            tbSymbolSet.Text = graphic.Attributes["SET"].ToString();
            tbXaml.Text = graphic.Attributes["XAML"].ToString();
        }

        private void btnRefresh_Click_1(object sender, RoutedEventArgs e)
        {
            string xaml = tbXaml.Text;
            if (xaml.Trim() == string.Empty)
            {
                MessageBox.Show("please define the symbol !");
                tbXaml.Focus();
                return;
            }
            string err = null;
            try
            {
                Symbol newSymbol = compileSymbolfromXaml(xaml);
                if (newSymbol != null)
                {
                    SymbolPreview(newSymbol);
                    return;
                }
                err = "xaml has some problem ,but it without any syntax error";
            }
            catch (Exception ex) { err = ex.ToString(); }
            
            MessageBox.Show(err.Trim());
            tbXaml.Focus();
            return;
        }

        private Symbol compileSymbolfromXaml(string xaml)
        {
            string rdxaml = RDHander + xaml + RDFooter;
            ResourceDictionary rd = XamlReader.Parse(rdxaml) as ResourceDictionary;
            return rd[tbSymbolID.Text] as Symbol;
        }

        private void SymbolPreview(Symbol newSymbol)
        {                  
            
            //GraphicsLayer graphicslayer = MyMap.Layers["SymbolViewer"] as GraphicsLayer;
            //GraphicsLayer graphicslayer2 = MyMap.Layers["SymbolViewer2"] as GraphicsLayer;

            //graphicslayer.Graphics.Clear();
            //Graphic g = new ESRI.ArcGIS.Client.Graphic() 
            //{
            //     Symbol=newSymbol,
            //      Geometry=graphic.Geometry
            //};
            //graphic = g;
            graphic.Symbol = newSymbol;
            //graphicslayer.Refresh();
            //graphicslayer.Visible = false;
            //graphicslayer2.Graphics.Clear();
            //graphicslayer2.Graphics.Add(new Graphic()
            //                                {
            //                                    Geometry = graphic.Geometry,
            //                                    Symbol = newSymbol
            //                                });
            ////graphicslayer.Graphics.Add(graphic);

            //MyMap.Extent = graphicslayer2.FullExtent;
        }



    }
}
